home *** CD-ROM | disk | FTP | other *** search
/ Cream of the Crop 11 / Cream of the Crop 11-1.iso / educate / wordy402.zip / OFSET.C < prev    next >
C/C++ Source or Header  |  1995-12-17  |  7KB  |  251 lines

  1. /**************************************************************************/
  2. /*                             OFset   Utility                            */
  3. /*                                                                        */
  4. /*                                 M\Cooper                               */
  5. /*                        3425 Chestnut Ridge Rd.                         */
  6. /*                        Grantsville, MD 21536-9801                      */
  7. /*                        --------------------------                      */
  8. /*                        E-mail: thegrendel@aol.com                      */
  9. /*                                                                        */
  10. /*                $2.00 to register the entire WORDY package              */
  11. /*                                                                        */
  12. /**************************************************************************/
  13.  
  14. #include <conio.h>
  15. #include "srch.h"
  16.  
  17. #define FILE_OPENING_ERROR 3
  18. #define FILENAME_MAXLEN 8
  19. #define CR "\n"
  20. #define MAXLEN 30
  21. #define LINE_LEN 80
  22. #define NOARGS 1
  23. #define INCREMENT 1
  24. #define SPACE ' '
  25. #define WD 8
  26.  
  27. #define BUFFERSIZE 8192
  28.  
  29. char ad[] =
  30. "OFset utility by M\\Cooper, 3425 Chestnut Ridge Rd., Grantsville, MD 21536";
  31.  
  32. typedef enum { FALSE, TRUE } Boolean;
  33.  
  34. void getword( char *lset, char *filename, int l_number );
  35. void center( char *strng );
  36. Boolean wordtest( char *letterset, char *word, int number_of_letters_sel );
  37.  
  38.  
  39. void main( int argc, char **argv )
  40. {
  41.  
  42.    char letterset [ MAXLEN ],
  43.         filename [ MAXLEN ],
  44.         ans [WD];
  45.         int number;
  46.  
  47.      if( argc == NOARGS )
  48.         {
  49.         clrscr();
  50.         printf( "Enter a LETTERSET to select from ... " );
  51.         gets( letterset );
  52.      strcpy( filename, "word.lst" );
  53.      printf( "\nHow many letters to select from %s? ", letterset );
  54.      gets( ans );
  55.      number = atoi( ans );
  56.         }
  57.      else
  58.      if( argc == NOARGS + 1 )
  59.            {
  60.         strcpy( letterset, *( argv + 1 ) );
  61.         strcpy( filename, "word.lst" );
  62.         clrscr();
  63.         printf( "\nHow many letters to select from %s? ", letterset );
  64.         gets( ans );
  65.         number = atoi( ans );
  66.         }
  67.    else
  68.       if( argc == NOARGS + 2 )
  69.         {
  70.         strcpy( letterset, *( argv + 1 ) );
  71.         number = atoi( *( argv + 2 ) );
  72.         strcpy( filename, "word.lst" );
  73.         }
  74.  
  75.    else
  76.      {
  77.      strcpy( letterset, *( argv + 1 ) );
  78.      number = atoi( *( argv + 2 ) );
  79.      strcpy( filename,  *( argv + 3 ) );
  80.      }
  81.  
  82.      getword( letterset, filename, number );
  83. }
  84.  
  85.  
  86. /**********************************WORDTEST********************************/
  87. /*       Function tests if word is constructible from Letterset           */
  88. /*                 Args in: char *letterset, char *word                   */
  89. /*   Returns: error_flag == TRUE (1) if constructible, FALSE (0) if not   */
  90. /**************************************************************************/
  91.  
  92. Boolean wordtest( char *letterset, char *word, int lnumber )
  93. {
  94.    int hits = 0;
  95.  
  96.       while( *letterset )
  97.          {
  98.          if( strchr( word, *letterset ) )
  99.             hits++;
  100.  
  101.          letterset++;
  102.          }
  103.       
  104.       if( hits >= lnumber )
  105.          return( TRUE );
  106.       else
  107.          return( FALSE );
  108. }
  109.  
  110. /*************************************************************/
  111. void getword( char *letter_set, char *filename, int number )
  112. {
  113.  
  114.     char    l_set [ MAXLEN ],
  115.         word [ MAXLEN ],
  116.         tempstr [ LINE_LEN + 1 ],
  117.         targetfile [ MAXLEN ],
  118.         bar [ LINE_LEN + 1 ],
  119.    wd [ WD ],
  120.    ltr [ WD ],
  121.         double_bar [ LINE_LEN + 1 ],
  122.   file_suffix[] = ".ofs";
  123.  
  124.     FILE *fptr,
  125.         *tfile;
  126.     int fnamelen;
  127.     long wcount = 0L;
  128.  
  129.        memset( bar, '-', LINE_LEN );
  130.        *( bar + LINE_LEN ) = NULL;
  131.        memset( double_bar, '=', LINE_LEN );
  132.        *( double_bar + LINE_LEN ) = NULL;
  133.  
  134.       strcpy( l_set, letter_set);
  135.  
  136.        /*************opening credits*****************************/
  137.        clrscr();
  138.        printf( double_bar );
  139.        strcpy( tempstr, ad );
  140.        center ( tempstr );
  141.        printf( tempstr );
  142.        printf( CR );
  143.        printf( double_bar );
  144.        printf( CR );
  145.        /*********************************************************/
  146.        /*   Create name of file to store derived words in       */
  147.        /*********************************************************/
  148.        fnamelen = strlen( l_set );
  149.        if( fnamelen  > FILENAME_MAXLEN )
  150.           fnamelen = FILENAME_MAXLEN;
  151.        strncpy( targetfile, l_set, fnamelen );
  152.        *( targetfile + fnamelen ) = NULL;
  153.        //NULL-terminate string, so strcat works, ha, ha.
  154.        strcat( targetfile, file_suffix );
  155.       /*********************************************************/
  156.  
  157.        if( !( fptr = fopen( filename, "rt" ) ) )
  158.          {
  159.          printf( "\7\7\7Cannot open Wordfile!" );
  160.          exit( FILE_OPENING_ERROR );
  161.          }
  162.       if( setvbuf( fptr, NULL, _IOFBF, BUFFERSIZE ) )
  163.          exit( FILE_OPENING_ERROR + 1 );
  164.  
  165.        if( !( tfile = fopen( targetfile, "wt" ) ) )
  166.          {
  167.          printf( "\7\7\7Cannot open file to save words in!" );
  168.          exit ( FILE_OPENING_ERROR + 2 );
  169.          }
  170.       if( setvbuf( tfile, NULL, _IOFBF, BUFFERSIZE ) )
  171.          exit( FILE_OPENING_ERROR + 3 );
  172.  
  173.        /**************'Wait' Message************/
  174.        printf( CR CR );
  175.        printf( "WORKING...\n\n" );
  176.        printf( "This will take a few seconds...\n" );
  177.        printf( "Please be patient.\n\n" );
  178.        printf( "Now searching 100,000+ word file \nand writing file of valid words containing at least %d letter(s) of -%s-.\n\n", 
  179.             number, letter_set );
  180.        /*****************************************/
  181.  
  182.  
  183.        if( number == INCREMENT )
  184.            strcpy( ltr, "letter" );
  185.        else
  186.            strcpy( ltr, "letters" );
  187.  
  188.  
  189.        sprintf( tempstr, "Word(s) selected with at least %d %s from: %s.\n",
  190.             number, ltr, strupr( l_set ) );
  191.        center( tempstr );
  192.        fprintf( tfile, double_bar );
  193.       fprintf( tfile, CR );
  194.        fprintf( tfile, tempstr );
  195.        fprintf( tfile, double_bar );
  196.        fprintf( tfile, CR );
  197.  
  198.  
  199.          /*********************Main Loop*************/     
  200.           while( fgets( word, MAXLEN, fptr ) != NULL )
  201.  
  202.             if( wordtest( letter_set, word, number ) )
  203.                {
  204.                fprintf( tfile, "%s", word );
  205.                wcount++;
  206.                }
  207.           /*******************************************/
  208.  
  209.       if( wcount == INCREMENT )
  210.           strcpy( wd, "word" );
  211.       else 
  212.           strcpy( wd, "words" );
  213.  
  214.           fprintf( tfile, bar );
  215.       fprintf( tfile, CR );
  216.           sprintf( tempstr, "%ld %s can be constructed using at least %d %s from %s.",
  217.                  wcount, wd, number, ltr, l_set );
  218.           center( tempstr );              
  219.           fprintf( tfile, tempstr );
  220.           fprintf( tfile, "\n\n" );
  221.  
  222.           center( ad );
  223.           fprintf( tfile, ad );
  224.  
  225.           fcloseall();
  226.  
  227.           sprintf( tempstr,
  228.                  "The file %s has %ld %s containing at least %d %s of %s\7.\n\n",
  229.                  targetfile, wcount, wd, number, ltr, l_set );
  230.           center( tempstr );
  231.           printf( CR CR );
  232.           printf( tempstr );
  233.  
  234. }
  235.  
  236.  
  237.  
  238. void center( char *str )
  239. {
  240.    int padding;
  241.    char st [ LINE_LEN + INCREMENT ];
  242.  
  243.      padding = LINE_LEN / 2 - strlen( str ) / 2;
  244.      memset( st, SPACE, padding );
  245.      *( st + padding ) = NULL;  //Terminate string
  246.      strcat( st, str );
  247.      strcpy( str, st );
  248.  
  249.      return;
  250. }
  251.